package interview10;

/**
 * @author Jack
 * @date 2019-11-06-15:28
 * 求斐波那契数列的第n项
 */
public class Solution {

    public int getFibonacciN(int n){
        if(n <= 0){return 0;}
        if(n == 1 || n == 2){return n;}
        int fibonacciPreviousTwo = 1;
        int fibonacciPreviousOne = 2;
        int fibonacciN = 0;
        for (int i = 3; i <= n; i++) {
            fibonacciN = fibonacciPreviousOne + fibonacciPreviousTwo;
            if(i == n){break;}
            fibonacciPreviousTwo = fibonacciPreviousOne;
            fibonacciPreviousOne = fibonacciN;
        }
        return fibonacciN;
    }

}
